﻿/* $RCSFile: XrmEmailing.asmx.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/AddonNice.EMailing/Services/XrmEmailing.asmx.cs $
 * $log$
 * Revision 23 2010/08/10 17:58:14  christian.surieux
 *   Missing comments in asmx file
 * Revision 10 2010/08/09 10:13:18  christian.surieux
 *   Added Log comment to all cs files
 */

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

using AddonNice.UI;
using AddonNice.Design;
using AddonNice.UI.WebControls;
using AddonNice.Security;
using AddonNice.UI.DataTypes;
using AddonNice.Configuration;
using AddonNice.Diagnostics;
using AddonNice.Settings;

using AddonNice.CRMWrapper;
using AddonNice.CRMWrapper.Design;
using AddonNice.CRMEMailing;
using AddonNice.CRMEMailing.Settings.UI.WebControls;
using System.Globalization;
using CrmSdk;

using Telerik.Web.UI;

using Trace2    =   System.Diagnostics.Trace;
using Cache2    =   AddonNice.Settings.Cache.CurrentCache;


namespace AddonNice.Services
{
    /// <summary>
    /// Acces to DownloadSessions for loading RadComboboxes
    /// </summary>
    [WebService(Namespace = "http://addonnice.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class XrmEmailing : WebService
    {
        protected CRMWrapperBase Wrapper
        {
            get
            {
                return AddonNice.Security.PortalSecurity.Wrapper;
            }
        }

        protected void ResetWrapper(DynEntity dynEnt)
        {
            CRMWrapperBase  w   =   Wrapper;
            if ( !dynEnt.HasWrapper(w) )
                dynEnt.SetWrapper(w);
        }

        [WebMethod]
        public RadComboBoxItemData[] GetDownloadSessions(object context)
        {
            if (PrivateDocFeederTrace.Sw.TraceVerbose)
                Trace2.WriteLine("[V]XrmEmailing GetDownloadSessions BEG.",PrivateDocFeederTrace.Sw.Info);
            return GetEntity(context,AStr.adndownloadsession,AStr.adndownloadsessionid,AStr.adntitle);
        }

        [WebMethod]
        public RadComboBoxItemData[] GetPrivateDocuments(object context)
        {
            return GetEntity(context,AStr.adnprivatedocument,AStr.adnprivatedocumentid,AStr.adntitle);
        }

        [WebMethod]
        public RadComboBoxItemData[] GetEntity(object context,string entName,string primaryKey,string displayKey)
        {
            // We cannot use a dictionary as a parameter, because it is only supported by script services.
            // The context object should be cast to a dictionary at runtime.
            IDictionary<string, object> contextDictionary   =   (IDictionary<string, object>)context;
		    string filterString                             =   ((string)contextDictionary["FilterString"]).ToLower()+"%";
            if (PrivateDocFeederTrace.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]XrmEmailing GetEntity BEG filterString: {0}", filterString),PrivateDocFeederTrace.Sw.Info);
            List<CrmListItem> lst                           =   Wrapper.Util.EntitiesLike(entName,primaryKey,displayKey,filterString,100,true);
            RadComboBoxItemData[] result                    =   new RadComboBoxItemData[lst.Count];
		    for (int i=0 ; i < lst.Count ; i++)
		    {
                CrmListItem it                              =   lst[i];
			    RadComboBoxItemData itemData                =   new RadComboBoxItemData();
                itemData.Text                               =   it.name;
                itemData.Value                              =   it.ID.ToString();
			    result[i]                                   =   itemData;
		    }
            if (PrivateDocFeederTrace.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]XrmEmailing GetEntity END lst.Count: {0}", lst.Count),PrivateDocFeederTrace.Sw.Info);
		    return result;
        }
    }
}
